<style></style>
<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="16">
        <div class="grid-content ep-bg-purple" />
        <el-input v-model="search" placeholder="Please input" />
      </el-col>
      <el-col :span="8">
        <div class="grid-content ep-bg-purple" />
        <el-button type="primary" plain @click="searchInfo()">搜索</el-button>
        <el-button type="primary" plain @click="dialogFormVisible = true">添加</el-button>
      </el-col>
    </el-row>
    <el-table :data="filterTableData" style="width: 100%">
      <el-table-column label="Cid" prop="Cid" />
      <!-- <el-table-column label="tname" prop="tname" /> -->
      <el-table-column label="Cname" prop="Cname" />
      <el-table-column align="right">

        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <div class="demonstration" style="margin-top: 5px;"></div>
      <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[3, 5, 10, 20]"
        :small="small" :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper"
        :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>

  <el-dialog v-model="dialogFormVisible" title="Shipping address" width="500" :before-close="handleBeforeClose" :close-on-click-modal="false">
    <el-form :model="form">
      <el-form-item label="编号" :label-width="formLabelWidth">
        <el-input v-model="form.Cid" autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth">
        <el-input v-model="form.Cname" autocomplete="off" />
      </el-form-item>
      <el-form-item label="类别" :label-width="formLabelWidth">
        <el-select v-model="form.Pid" class="m-2" placeholder="-----请选择-----" size="large" style="width: 280px">
          <el-option v-for="item in options" :key="item.PID" :label="item.PNUMBER" :value="item.PID" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save()">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup >
import { ref, onMounted, reactive } from 'vue'
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus'

const search = ref('')
const filterTableData = ref([])

const currentPage4 = ref(1)
const pageSize4 = ref(3)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
var total = ref(0)

var options = ref([])

//切换页条数
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  bindData(search.value, currentPage4.value, pageSize4.value);
}
//切换页数
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  bindData(search.value, currentPage4.value, pageSize4.value);
}

//添加
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'

var form = ref({
  Cid: '',
  Pid: '',
  Cname: ""
})

//保存
function save() {
  dialogFormVisible.value = false;


  axios({
    url: form.value.Cid == 0 ? "https://localhost:7010/api/Values/Post2" : "https://localhost:7010/api/Values/Put",
    method: form.value.Cid == 0 ? "post" : "put",
    data: form.value,
    headers: {
      "Content-Type": "application/json"
    }
  }).then((res) => {
    if (res.data) {
      ElMessage({
        type: 'success',
        message: (form.value.Cid == 0 ? "添加" : "编辑") + '成功',
      })
      clearForm();
      bindData(search.value, currentPage4.value, pageSize4.value);
    } else {
      ElMessage({
        type: 'warning',
        message: (form.value.Cid == 0 ? "添加" : "编辑") + '失败',
      })
    }
  })
}

//编辑
const handleEdit = (index, row) => {
  console.log(index, row);
  form.value = row;
  //form = row;
  console.log(form);
  dialogFormVisible.value = true;
}

const handleBeforeClose = (done) => {
  clearForm();
  done(); // 关闭对话框
}



//删除
const handleDelete = (index, row) => {
  ElMessageBox.confirm(
    '确定要删除吗?',
    'Warning',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      axios.delete("https://localhost:7010/api/Values/Delete?id=" + row.Cid)
        .then(function (res) {
          if (res.data) {
            ElMessage({
              type: 'success',
              message: '删除成功',
            })
            bindData(search.value, currentPage4.value, pageSize4.value);
          } else {
            ElMessage({
              type: 'warning',
              message: '删除失败',
            })
          }
        })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '用户取消操作',
      })
    })
}

onMounted(() => {
  bindData(search.value, currentPage4.value, pageSize4.value);
  axios.get("https://localhost:7010/api/Values/getById").then(function (res) {
    options.value = res.data;
  })
})

//条件查询数据
function searchInfo() {
  currentPage4.value = 1;
  bindData(search.value, currentPage4.value, pageSize4.value);
}
//绑定数据
function bindData(strName, page, limit) {
  axios.get(`https://localhost:7010/api/Values/Get/getInfo?page=${page}&limit=${limit}&name=${strName}`).then((res) => {
    filterTableData.value = res.data.data
    total.value = res.data.total
  })
}

//清理表单
function clearForm() {
  form.value.Cid = '';
  form.value.Pid = '';
  form.value.Cname = '';
}

</script>